package cn.xfdzx.android.apps.shop.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.XFDGoodsDeatailsActivity;
import cn.xfdzx.android.apps.shop.bean.MoreGoodsBean;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.util.EmptyUtil;
import cn.xfdzx.android.apps.shop.util.ImageGlideUtil;

/**
 * @author gzk
 * @date 2020/4/13 0013
 * description：零售推荐
 */
public class ShopTuiJianAdapter extends RecyclerView.Adapter<ShopTuiJianAdapter.MyViewHolder> {

    private ItemClickListener mItemClickListener;
    private List<MoreGoodsBean.DataBean> list = new ArrayList<>();
    private Context context;

    public void setList(List<MoreGoodsBean.DataBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public ShopTuiJianAdapter(Context context, List<MoreGoodsBean.DataBean> list) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_shop_details_tuijian, parent, false);
        MyViewHolder myHolder = new MyViewHolder(inflate);

        return myHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
        if (EmptyUtil.isNotEmpty(list)){
            holder.tvShopTuijianName.setText(list.get(position).getTitle());
            if (Double.parseDouble(list.get(position).getDiscountprice())==0.0){
                holder.tvBox.setText("¥"+list.get(position).getMarketprice()+"/"+list.get(position).getUnit());
            }else {
                holder.tvBox.setText("¥"+list.get(position).getDiscountprice()+"/"+list.get(position).getUnit());
            }
            RoundedCorners roundedCorners = new RoundedCorners(20);
            //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
            RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
            options.placeholder(R.drawable.icon_cover)//图片加载出来前，显示的图片
                    .fallback(R.drawable.icon_cover) //url为空的时候,显示的图片
                    .error(R.drawable.icon_cover);
            Glide.with(context).load(list.get(position).getThumb()).apply(options.transform(new CenterCrop(), roundedCorners)).into(holder.ivShopTuijian);

        }

//  头部标签
//        holder.ttvContent.setSingleTagAndContent("24小时发货", "泰国小菠萝凤梨新鲜水果鲜果批发");
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mItemClickListener.onItemClick(position);
            }
        });
    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_shop_tuijian)
        ImageView ivShopTuijian;
        @BindView(R.id.tv_shop_tuijian_name)
        TextView tvShopTuijianName;
        @BindView(R.id.tv_box)
        TextView tvBox;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    public void setOnItemClickListener(ItemClickListener itemClickListener) {
        this.mItemClickListener = itemClickListener;

    }

}
